package cn.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBCUtils {
    /**
     *获取数据库的连接
     * @return Connection
     * @throws Exception
     */
    public static Connection getConnection() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        String url ="jdbc:mysql://localhost:3306/jdbctest?characterEncoding=utf8";
        String name="root";
        String pwd="654321";
        Connection conn= DriverManager.getConnection(url,name,pwd);
        return conn;
    }

/**
 * 关闭JDBC的资源
 * @param rs 结果集
 * @param ps 传输器
 * @param conn 连接
 */
   public static void close(ResultSet rs, PreparedStatement ps,Connection conn){
       try{
           rs.close();
       }catch(Exception e){
           e.printStackTrace();
       } finally {
           rs=null;
       }

       try{
           ps.close();
       }catch(Exception e){
           e.printStackTrace();
       } finally {
           ps=null;
       }

       try{
           conn.close();
       }catch(Exception e){
           e.printStackTrace();
       } finally {
           conn=null;
       }
   }

}


